using Chromely.Core.Network;
using MyChromelyApp1.Models;
using MyChromelyApp1.Utils;

namespace MyChromelyApp1.ChromelyControllers
{
    /// <summary>
    /// 笔记控制器
    /// </summary>
    [ChromelyController(Name = "NoteController")]
    public class NoteController : ChromelyController
    {

        /// <summary>
        /// 创建临时笔记
        /// </summary>
        /// <param name="currentNotebook">当前笔记本</param>
        /// <returns></returns>
        [ChromelyRoute(Path = "/notecontroller/createnote")]
        public ResData CreateNote(string currentNotebook)
        {
            var name = DateTime.Now.ToLocalTime().ToString("yyyyMMddHHmmss");
            var path = Path.Combine(MyConfig.localData, currentNotebook, (name + ".md"));
            try
            {
                var f = File.Create(path);
                f.Dispose();
                return new ResData(200, "创建成功", name);
            }
            catch (Exception e)
            {
                return new ResData(500, "创建失败：" + e.Message);
            }
        }


        /// <summary>
        /// 打开笔记
        /// </summary>
        /// <param name="currentNotebook">当前笔记本</param>
        /// <param name="currentNote">当前笔记</param>
        /// <returns></returns>
        [ChromelyRoute(Path = "/notecontroller/opennote")]
        public ResData OpenNote(string currentNotebook, string currentNote)
        {
            var path = Path.Combine(MyConfig.localData, currentNotebook, (currentNote + ".md"));
            try
            {
                // 读取笔记内容
                string noteContent = File.ReadAllText(path);
                return new ResData(200, "读取成功", noteContent);
            }
            catch (Exception e)
            {
                return new ResData(500, "读取失败：" + e.Message);
            }
        }


        /// <summary>
        /// 保存笔记内容
        /// </summary>
        /// <param name="currentNotebook">当前笔记本</param>
        /// <param name="currentNote">当前笔记</param>
        /// <param name="content">笔记内容</param>
        /// <returns></returns>
        [ChromelyRoute(Path = "/notecontroller/savenote")]
        public ResData SaveNote(string currentNotebook, string currentNote, string noteTitle, string content)
        {
            var path = Path.Combine(MyConfig.localData, currentNotebook, (currentNote + ".md"));
            // 新路径
            var newPath = Path.Combine(MyConfig.localData, currentNotebook, (noteTitle + ".md"));
            try
            {
                // 写入笔记内容
                File.WriteAllText(path, content);
                // 文件重命名
                File.Move(path, newPath);
                return new ResData(200, "保存成功");
            }
            catch (Exception e)
            {
                return new ResData(500, "保存失败：" + e.Message);
            }
        }


        /// <summary>
        /// 删除笔记
        /// </summary>
        /// <param name="currentNotebook">当前笔记本</param>
        /// <param name="currentNote">要删除的笔记</param>
        /// <returns></returns>
        [ChromelyRoute(Path = "/notecontroller/removenote")]
        public ResData RemoveNote(string currentNotebook, string currentNote)
        {
            var path = Path.Combine(MyConfig.localData, currentNotebook, (currentNote + ".md"));
            try
            {
                // 删除文件
                File.Delete(path);
                return new ResData(200, "删除成功");
            }
            catch (Exception e)
            {
                return new ResData(500, "删除失败：" + e.Message);
            }
        }

    }
}
